<template>
  <div class="proDialog">
    <el-dialog title="选择商品" :visible.sync="visible" width="800px" z-index="1500" append-to-body @close="toCancelBox">
      <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px"
        style="padding-top: 0">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="商品分类" prop="categoryUid">
          <el-cascader :props="{ checkStrictly: true }" v-model="queryParams.categoryUid" :options="categoryOptions"
            clearable filterable size="small" class="input-rule">
          </el-cascader>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table border ref="indexTable" :data="infoList" tooltip-effect="dark" style="width: 100%"
        @selection-change="handleSelectionChange" :row-key="bindRowKeys">
        <el-table-column type="selection" align="center" :reserve-selection="true">
        </el-table-column>
        <el-table-column prop="carouselImgOne" label="图片" align="center" width="100">
          <template slot-scope="scope">
            <el-image v-if="scope.row.carouselImgOne != null &&
              scope.row.carouselImgOne != ''
              " slot="reference" :src="scope.row.carouselImgOne" class="image-rows"
              :preview-src-list="[scope.row.carouselImgOne]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="productName" width="400" label="商品名称" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column prop="retailPriceStr" label="零售价(元)" align="center"></el-table-column>
        <el-table-column prop="stockNumber" label="商品库存" align="center"></el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" :page-sizes="pageSizess" />

      <div style="display: flex; justify-content: flex-end">
        <el-button @click="determine" type="primary">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listInfo } from "@/api/product/info";
import { selectOptions } from "@/api/product/category";

export default {
  props: {
    flag: {
      type: Boolean,
    },
    proUids: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  watch: {
    flag (val) { },
    proUids (val) {
      this.uids = JSON.parse(JSON.stringify(val));
    },
  },
  data () {
    return {
      // 显示搜索条件
      showSearch: true,
      datas: null,
      selectedDatas: [],
      pageSizess: [5, 10, 15, 20, 25, 30, 50],
      // 商品信息表格数据
      infoList: [],
      // indexTable: [],
      infoListHistory: [],
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        productName: null,
        categoryUid: null,
        brandUid: null,
        minBuyNum: null,
        maxBuyNum: null,
        minCostPirce: null,
        maxCostPirce: null,
        minRetailPrice: null,
        maxRetailPrice: null,
        minTradePrice: null,
        maxTradePrice: null,
        dataStatus: null,
        auditStatus: null,
        stockNumber: null,
        categoryUidOne: null,
        categoryUidTwo: null,
        categoryUidThree: null,
        dataStatus: "1",
        auditStatus: "1",
        uids: [], //所有已上架的活动商品ID
      },
      visible: false,
      categoryOptions: [],
    };
  },
  created () {
    this.uids = JSON.parse(JSON.stringify(this.proUids));
    this.visible = this.flag;
    this.getList();
    selectOptions().then((response) => {
      this.categoryOptions = response.data;
    });
  },
  methods: {
    // cascaderChange(value) {
    //   if (value.length == 0) {
    //     this.queryParams.categoryUid = null;
    //   }
    //   console.log(value);
    // },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    toCancelBox () {
      this.visible = false;
      this.$emit("toCloseBox", false);
    },
    //回显选中
    greet (selectedData) {
      this.selectedDatas = selectedData;
      this.selectedDatas.forEach((item) => {
        this.$refs.indexTable.toggleRowSelection(item, true);
      });
    },
    bindRowKeys (row) {
      return row.productUid;
    },
    handleSelectionChange (val) {
      this.datas = this.multipleSelection;
      this.multipleSelection = val;
      if (this.datas.length > 0) {
        for (let i = 0; i < this.datas.length; i++) {
          for (let k = 0; k < this.multipleSelection.length; k++) {
            if (
              this.datas[i].productUid == this.multipleSelection[k].productUid
            ) {
              this.multipleSelection[k] = this.datas[i];
            }
          }
        }
      }
    },
    /** 确定选择商品 */
    determine () {
      this.$emit("multipleSelection", this.multipleSelection);
      this.infoList.forEach((item) => {
        this.$refs.indexTable.toggleRowSelection(item, false);
      });
      this.$forceUpdate();
      this.open = false;
    },
    /** 查询商品信息列表 */
    getList () {
      if (
        this.queryParams.categoryUid != null &&
        this.queryParams.categoryUid.length > 0
      ) {
        let uidArray = this.queryParams.categoryUid;
        this.queryParams.categoryUidOne = uidArray[0];
        if (uidArray.length > 1) {
          this.queryParams.categoryUidTwo = uidArray[1];
        }
        if (uidArray.length > 2) {
          this.queryParams.categoryUidThree = uidArray[2];
        }
      } else {
        this.queryParams.categoryUidOne = null;
        this.queryParams.categoryUidTwo = null;
      }
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>
<style scoped>
.determine {
  position: relative;
  color: #fff;
}

.pagination-container {
  position: relative;
}

.image-rows {
  width: 80px;
  height: 80px;
}
</style>
